<template>
  <div class="app-container">

    <h2 class="steps-title">发布新课程</h2>

    <el-steps :active="active" finish-status="success" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="发布课程"/>
    </el-steps>

    <div class="course-publish-info">
      <img :src="coursePublish.cover">
      <div class="main">
        <h2>{{ coursePublish.name }}</h2>
        <p>课程共{{ coursePublish.lessonSum }}个课时</p>
        <p>所属一级分类：{{ coursePublish.parentCategory }}</p>
        <p>所属二级分类：{{ coursePublish.category }}</p>

        <p v-if="coursePublish.level === 0">课程等级：{{ `入门` }}</p>
        <p v-if="coursePublish.level === 1">课程等级：{{ `初级` }}</p>
        <p v-if="coursePublish.level === 2">课程等级：{{ `中级` }}</p>
        <p v-if="coursePublish.level === 3">课程等级：{{ `高级` }}</p>

        <p v-if="coursePublish.price !== 0">课程售价：￥{{ coursePublish.price }} 元</p>
        <p v-if="coursePublish.price === 0">课程售价：免费</p>
      </div>
    </div>

    <div class="steps-bottom">
      <el-button @click="previous">返回修改</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="publish">发布课程</el-button>
    </div>
  </div>
</template>

<script>
  import { publishCourseById, getCoursePublishById } from '@/api/course'

  export default {
    data() {
      return {
        active: 2,
        saveBtnDisabled: false, // 保存按钮是否禁用
        courseId: '',
        coursePublish: {}
      }
    },

    created() {
      this.init()
    },

    methods: {

      init() {
        if (this.$route.params && this.$route.params.id) {
          this.courseId = this.$route.params.id
          // 根据id获取课程基本信息
          this.fetchCoursePublishInfoById()
        }
      },

      fetchCoursePublishInfoById() {
        getCoursePublishById(this.courseId).then(result => {
          this.coursePublish = result.data.item
        })
      },

      previous() {
        this.$router.push({ path: '/manage/course/chapter/' + this.courseId })
      },

      publish() {
        publishCourseById(this.courseId, this.$qs.stringify({status: 0})).then(result => {
          this.$message.success("发布成功！")
          this.$router.push({ path: '/manage/course/list' })
          this.active = 3
        })
      }
    }
  }
</script>
<style scoped>
  .steps-title{
    padding: 20px;
    text-align: center;
  }
  .steps-bottom{
    text-align: center;
    padding: 20px;
  }
  .course-publish-info {
    background: #f5f5f5;
    padding: 20px;
    overflow: hidden;
    border: 1px dashed #DDD;
    margin-bottom: 40px;
    position: relative;
  }
  .course-publish-info img {
    background: #d6d6d6;
    width: 500px;
    height: 278px;
    display: block;
    float: left;
    border: none;
  }
  .course-publish-info .main {
    margin-left: 520px;
  }

  .course-publish-info .main h2 {
    font-size: 28px;
    margin-bottom: 30px;
    line-height: 1;
    font-weight: normal;
  }
  .course-publish-info .main p {
    margin-bottom: 10px;
    word-wrap: break-word;
    line-height: 24px;
    max-height: 48px;
    overflow: hidden;
  }

  .course-publish-info .main p {
    margin-bottom: 10px;
    word-wrap: break-word;
    line-height: 24px;
    max-height: 48px;
    overflow: hidden;
  }
  .course-publish-info .main h3 {
    left: 540px;
    bottom: 20px;
    line-height: 1;
    font-size: 28px;
    font-weight: normal;
    position: absolute;
  }
</style>
